package ztc.view;

import ztc.control.myex;
import ztc.service.ConApi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.Map;

/**
 * Created by ztc on 15-11-18.
 */
public class Display extends JFrame implements KeyListener,Runnable {

    JTextArea basic,now,aqi,daily_forecast,hourly_forecast,suggestion;
    JTextField jtf;
    JPanel jp;
    JTabbedPane jtp;
    JScrollPane jsp,jsp1,jsp2;
    Boolean running;
    Thread t;
    public static void main(String[] args){
        new Display();
    }
    public Display(){
        running=false;
        //顶部
        jtf=new JTextField(15);
        jtf.setText("请输入城市全拼,回车确定！");
        jtf.addKeyListener(this);
        basic=new JTextArea();
        jp=new JPanel();
        jp.setLayout(new GridLayout(2,1));
        jp.add(jtf);
        jp.add(basic);
        //选项卡now
        jtp=new JTabbedPane();
        //now
        now=new JTextArea();
        jtp.add("现状",now);
        //aqi
        aqi=new JTextArea();
        jtp.add("空气质量",aqi);
        //daily_forecast
        daily_forecast=new JTextArea();
        jsp1=new JScrollPane(daily_forecast);
        jtp.add("最近七天",jsp1);
        //hourly_forecast
        hourly_forecast=new JTextArea();
        jsp2=new JScrollPane(hourly_forecast);
        jtp.add("最近3小时",jsp2);
        //suggestion
        suggestion=new JTextArea();
        jsp=new JScrollPane(suggestion);
        jtp.add("建议",jsp);


        this.add(jtp);
        this.add(jp,"North");


        URL url=Display.class.getResource("/img/icon.gif");
        this.setIconImage(new ImageIcon(url).getImage());
        this.setLocation(500,300);
        this.setSize(300,400);
        this.setTitle("天气_:ZTC");
        this.setVisible(true);
        //this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void showBasic(String msg){
        basic.setForeground(java.awt.Color.BLUE);
        basic.setText(msg);
    }
    public void showNow(String msg){
        now.setFont(new Font(null,Font.BOLD,20));
        if(msg.equals(""))
            now.setText("未知城市，请检查拼写。");
        else
            now.setText(msg);
    }
    public void showAqi(String msg){
        if(msg.equals(""))
            aqi.setText("该地区无此选项。");
        else
            aqi.setText(msg);
    }
    public void showSuggestion(String msg){
        if(msg.equals(""))
            suggestion.setText("该地区无此选项。");
        else
            suggestion.setText(msg);
    }
    public void showHourly(String msg){
        hourly_forecast.setFont(new Font(null, Font.CENTER_BASELINE, 15));
        hourly_forecast.setText(msg);
    }
    public void showDaily(String msg){
        daily_forecast.setFont(new Font(null, Font.LAYOUT_LEFT_TO_RIGHT, 15));
        daily_forecast.setText(msg);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_ENTER){
            now.setText("");
            ConApi.clearAll();
            Map map=ConApi.getAll(jtf.getText());
            showNow(myex.getNow(map));
            showBasic(myex.getBasic(map));
            showAqi(myex.getAqi(map));
            showSuggestion(myex.getSuggestion(map));
            showHourly(myex.getHourly(map));
            showDaily(myex.getDaily(map));
            if(!running){
                t=new Thread(this);
                t.start();
                running=true;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        int i=0;
        while(true) {
            i++;
            now.setText("");
            ConApi.clearAll();
            Map map = ConApi.getAll(jtf.getText());
            showNow(myex.getNow(map));
            showBasic(myex.getBasic(map)+"No. "+i+" times");
            showAqi(myex.getAqi(map));
            showSuggestion(myex.getSuggestion(map));
            showHourly(myex.getHourly(map));
            showDaily(myex.getDaily(map));
            try {
                t.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
